#问答
from flask import Blueprint,request,render_template,redirect,url_for,g
from .forms import QuestionForm,AnswerForm
from .decorators import login_required
from models import QuestionModel,AnswerModel
from exts import db

bp=Blueprint('qa',__name__,url_prefix='/')#作为主页

@bp.route('/')
def index():
    #http://127.0.0.1:5000/?page=1 加分页
    page=request.args.get('page',1)#可能是空
    # print('page=',page)
    page=int(page)
    #page!='None':
    paginate=QuestionModel.query.order_by(QuestionModel.create_time.desc()).paginate(page=page,per_page=3)#分页器对象

    #print(paginate.pages)#一共多少页
    #print(paginate.page)#当前页
    # print(paginate.has_prev)  # 判断是否有上一页
    # print(paginate.has_next)#判断是否有下一页
    # print(paginate.prev_num)  # 上一页的页码
    # print(paginate.next_num)#下一页的页码
    # print(paginate.items)#当前页所有项

    #questions=QuestionModel.query.order_by(QuestionModel.create_time.desc()).all()#倒序，过多可用分页
    return render_template('index.html',paginate=paginate)#questions=questions


@bp.route('/qa/public',methods=['GET','POST'])
@login_required
def public_question():
    if not g.user:
        return redirect('/auth/login')
    if request.method=='GET':
        return render_template('public_question.html')
    else:
        form=QuestionForm(request.form)
        if form.validate():
            title=form.title.data
            content=form.content.data
            question=QuestionModel(title=title,content=content,author=g.user)
            db.session.add(question)
            db.session.commit()
            return redirect('/')
        else:
            print(form.errors)
            return redirect(url_for('qa.public_question'))#/qa/public

@bp.route('/qa/detail/<qa_id>')
def qa_detail(qa_id):
    question=QuestionModel.query.get(qa_id)
    return render_template('detail.html',question=question)

@bp.post('/answer/public')#@bp.route('/answer/public',methods=['POST'])
@login_required
def public_answer():
    form=AnswerForm(request.form)
    if form.validate():
        content=form.content.data
        question_id=form.question_id.data
        answer=AnswerModel(content=content,question_id=question_id, author_id=g.user.id)
        db.session.add(answer)
        db.session.commit()
        return redirect(url_for('qa.qa_detail',qa_id=question_id))
    else:
        print(form.errors)
        return redirect(url_for('qa.qa_detail',qa_id=request.form.get('question_id')))

@bp.route('/search')
def search():
    #这里用方式1 /search?q=flask
    #方式2 /search/<q>
    #post, request.form
    q=request.args.get("q")

    page = request.args.get('page', 1)  # 可能是空
    #print('page=', page)
    page = int(page)
    #questions=QuestionModel.query.filter(QuestionModel.title.contains(q)).all()
    #return render_template('index.html',questions=questions)
    paginate = QuestionModel.query.filter(QuestionModel.title.contains(q)).order_by(QuestionModel.create_time.desc()).paginate(page=page, per_page=3)

    return render_template('index2.html', paginate=paginate,q=q)
